package org.example.sample2.dao;

import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

@Getter
@Setter
public class Supermarket {
    String name;
    HashMap<String, FruitPack> storage;
    HashMap<String, FruitPack> bucketStorage;
    HashMap<String, FruitPack> saleStorage;
    ReentrantLock storageLock = new ReentrantLock();

    public Supermarket(String name, HashMap<String, FruitPack> storage) {
        this.name = name;
        this.storage = storage;
        this.bucketStorage = new HashMap<>();
        this.saleStorage = new HashMap<>();

    }

    public BigDecimal sell(Customer customer) {

        storageLock.lock();
        try {
            for (FruitPack fruit : customer.getBucket().values()) {
                FruitPack fruitInSaleStorage = saleStorage.get(fruit.getName());
                FruitPack fruitInBucketStorage = bucketStorage.get(fruitInSaleStorage.getName());
                if (fruitInSaleStorage == null) {
                    saleStorage.put(fruit.getName(), fruitInSaleStorage);
                    bucketStorage.put(fruit.getName(), fruitInSaleStorage);
                }else {
                    bucketStorage.put(fruit.getName(), fruitInSaleStorage);
                }
                if (fruitInBucketStorage == null) {
                    throw new RuntimeException();
                }
            }
        }
        catch (Exception e) {
            for (FruitPack fruit : saleStorage.values()) {
                FruitPack fruitInBucketStorage = bucketStorage.get(fruit.getName());
                if (fruitInBucketStorage == null) {
                    bucketStorage.put(fruit.getName(), fruit);
                }else {
                    fruitInBucketStorage.setNum(fruitInBucketStorage.getNum().add(fruit.getNum()));
                    bucketStorage.put(fruit.getName(),fruitInBucketStorage);
                }

            }
        }
        finally {
            storageLock.unlock();
        }


        return BigDecimal.ZERO;
    }

    public FruitPack storageIn(FruitPack fruitpacket) {
        storageLock.lock();

        try {
            if (fruitpacket == null) {
                return null;
            }
            if (fruitpacket.getNum() == null) {
                return null;
            }
            if (fruitpacket.getDiscount() == null) {
                fruitpacket.setDiscount(new BigDecimal(100));
            }
            if (fruitpacket.getDiscount().compareTo(BigDecimal.ZERO) <=0) {
                fruitpacket.setDiscount(new BigDecimal(100));
            }
            FruitPack fruitInStorage = storage.get(fruitpacket.getName());
            if (fruitInStorage == null) {
                fruitpacket.setMarketName(this.getName());
                storage.put(fruitpacket.getName(), fruitpacket);
                return fruitpacket;
            }else {
                fruitInStorage.setNum(fruitInStorage.getNum().subtract(fruitpacket.getNum()));
                storage.put(fruitInStorage.getName(), fruitInStorage);
            }
            return fruitInStorage;
        } finally {
            storageLock.unlock();
        }
    }
    public FruitPack storageOut(Customer customer, String fruitName, BigDecimal num) {
        storageLock.lock();
        try {
            FruitPack fruitInStorage = storage.get(fruitName);
            if (fruitInStorage == null) {
                return null;
            }else {
                if (fruitInStorage.getNum().compareTo(num) < 0) {
                    return null;
                }
                fruitInStorage.setNum(fruitInStorage.getNum().subtract(num));
                storage.put(fruitName, fruitInStorage);
            }
            FruitPack fruitInBucketStorage = bucketStorage.get(fruitName);
            FruitPack movedFruitPack = new FruitPack(fruitName,fruitInStorage.getPrice(),fruitInStorage.getDiscount(),num);
            if (fruitInBucketStorage ==null){
                bucketStorage.put(fruitName, movedFruitPack);
            }else {
                fruitInBucketStorage.setNum(fruitInBucketStorage.getNum().add(movedFruitPack.getNum()));
                bucketStorage.put(fruitName, fruitInBucketStorage);
            }
            return movedFruitPack;
        } finally {
            storageLock.unlock();
        }


    }

}
